package com.jiaofamily.localad.sdk.adapters;

import java.util.HashSet;
import java.util.Set;

import android.app.Activity;
import android.graphics.Color;
import android.util.Log;
import android.view.View;

import com.google.ads.Ad;
import com.google.ads.AdListener;
import com.google.ads.AdRequest;
import com.google.ads.AdRequest.ErrorCode;
import com.google.ads.AdSize;
import com.google.ads.AdView;
import com.jiaofamily.localad.sdk.LocalAdLayout;
import com.jiaofamily.localad.sdk.obj.Extra;
import com.jiaofamily.localad.sdk.obj.Ration;
import com.jiaofamily.localad.sdk.util.LocalAdUtil;

public class AdMobAdapter extends LocalAdAdapter implements AdListener{
	private static final String TAG=AdMobAdapter.class.getSimpleName();

	private Set set=null;
	private AdView adView=null;
	
	public AdMobAdapter(LocalAdLayout layout, Ration ration) {
		super(layout, ration);
		// TODO Auto-generated constructor stub
	}

	@Override
	public void handle() {
		// TODO Auto-generated method stub
		Log.d(TAG,"Enter into Admob");
		LocalAdLayout layout=this.layout.get();
		if(layout==null){
			return;
		}

		Activity activity=layout.activityReference.get();
		if(activity==null){
			return;
		}

		if(this.ration.key2!=null) {
			this.set=new HashSet<String>();
			String[] arrayOfString=this.ration.key2.split(",");
			for(int i=0;i<arrayOfString.length;i++) {
				this.set.add(arrayOfString[i]);
			}
		}
		
		AdRequest adRequest=new AdRequest();
		adRequest.setKeywords(this.set);
		
		adView=new AdView(activity,AdSize.BANNER,this.ration.key);
		adView.setAdListener(this);
		adView.loadAd(adRequest);

		Extra extra=layout.extra;
		int bgColor=Color.rgb(extra.bgRed, extra.bgGreen, extra.bgBlue);
		int fgColor=Color.rgb(extra.fgRed, extra.fgGreen, extra.fgBlue);
		adView.setBackgroundColor(bgColor);
		layout.manager.resetRollover();

		layout.handler.post(new LocalAdLayout.ViewAdRunnable(layout, adView));
		layout.rotateThreadedDelayed();

	}

	public void onDismissScreen(Ad arg0) {
		// TODO Auto-generated method stub
		Log.d(TAG,"Admob onDismissScreen");
	}

	public void onFailedToReceiveAd(Ad arg0, ErrorCode arg1) {
		// TODO Auto-generated method stub
		Log.d(TAG,"Admob onFailedToReceiveAd");
		arg0.setAdListener(null);
		
		LocalAdLayout layout=this.layout.get();
		if(layout==null){
			return;
		}
		//reduce weight
		if(LocalAdLayout.isDynamicAdjustWeight && layout.manager.getLanguage(layout.activityReference.get().getApplicationContext()).equals("zh")) {
			layout.manager.subWeight(LocalAdUtil.NETWORK_TYPE_ADMOB);
		}
		
		layout.removeView((View) arg0);
		layout.manager.resetRollover_pri();
		layout.rollover_pri();
	}

	public void onLeaveApplication(Ad arg0) {
		// TODO Auto-generated method stub
		Log.d(TAG,"Admob onLeaveApplication");
	}

	public void onPresentScreen(Ad arg0) {
		// TODO Auto-generated method stub
		Log.d(TAG,"Admob onPresentScreen");
	}

	public void onReceiveAd(Ad arg0) {
		// TODO Auto-generated method stub
		Log.d(TAG,"Admob onReceiveAd");
		Count.countShow(this.layout.get().getContext(),this.layout.get().activeRation);
//		LocalAdLayout layout=this.layout.get();
//		if(layout==null){
//			return;
//		}
		
//		layout.manager.resetRollover();
//		if(this.adView!=null){
//			layout.handler.post(new LocalAdLayout.ViewAdRunnable(layout, this.adView));
//		}
//		layout.rotateThreadedDelayed();
	}

}
